/**
 * =====================================================================================
 *       @file  ngx_shmtx.c
 *      @brief  
 *
 *  Detailed description starts here.
 *
 *   @internal
 *     Created  2012年05月11日 10时56分55秒 
 *    Revision  1.0.0.0
 *    Compiler  gcc/g++
 *
 *     @author  luis
 * =====================================================================================
 */

#include "ngx_shmtx.h"
#include "ngx_process_cycle.h"

ngx_int_t ngx_shmtx_create(ngx_shmtx_t *mtx, ngx_shmtx_sh_t *addr)
{
    mtx->lock = &addr->lock;  

    if (mtx->spin == (ngx_uint_t)-1)
    {
        return NGX_OK; 
    }

    mtx->spin = 2048;

    return NGX_OK;
}

void ngx_shmtx_destroy(ngx_shmtx_t *mtx)
{

}

ngx_int_t ngx_shmtx_trylock(ngx_shmtx_t *mtx)
{
    return (*mtx->lock == 0 && ngx_atomic_cmp_set(mtx->lock, 0, ngx_pid)); 
}

void ngx_shmtx_unlock(ngx_shmtx_t *mtx)
{
    ngx_atomic_cmp_set(mtx->lock, ngx_pid, 0);
}
